<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>路由管理</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/layui/layui.js"></script>
    <script src="/static/jquery/jquery-3.3.1.min.js"></script>
</head>
<body>
<form action="#" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label>名称:</label>
            <div class="layui-inline">
                <input type="text" name="gatewayapiname" autocomplete="off" placeholder="请输入名称"
                       class="layui-input">
            </div>
            状态:
            <div class="layui-inline">
                <select name="state">
                    <option value="">请选择</option>
                    <option value="0">无效</option>
                    <option value="1">有效</option>
                </select>
            </div>
            <button class="layui-btn" lay-submit lay-filter="gateway-table">搜索</button>
        </div>
    </div>
</form>
<script type="text/html" id="toolbar-header">
    <button lay-event="add" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe654</i>添加</button>
    <button lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><i class="layui-icon">&#xe640</i>删除</button>
</script>
<table class="layui-table" lay-filter="gateway-table" id="gateway-table"></table>
<script type="text/javascript">
    //1.页面加载时，需要显示第一页的数据
    layui.use(['table','form','layer'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer=layui.layer;
        //第一个实例
        table.render({
            elem: '#gateway-table'
            , url: '/apimapping/table' //数据接口
            , toolbar: "#toolbar-header"
            , page: true //开启分页
            , cols: [[ //表头
                {checkbox:true,fixed: 'left' },
                {field: 'id', title: 'ID', width: 70, sort: true }
                , {field: 'gatewayapiname', title: '名称', width: 120}
                , {field: 'insideapiurl', title: 'URL', width: 200, sort: true}
                , {field: 'serviceid', title: 'serviceId', width: 220}
                , {field: 'description', title: '描述', width: 177}
                , {field: 'state', title: '状态', width: 100, sort: true, templet:function(d){
                    return d.state==1?"<span class='layui-badge layui-bg-green'>有效</span>":"<span class='layui-badge layui-bg-red'>无效</span>";
                    }}
                , {field: 'idempotents', title: '幂等性', width: 100, sort: true,templet:function(d){
                            return d.idempotents==1?"<span class='layui-badge layui-bg-green'>幂等</span>":"<span class='layui-badge layui-bg-red'>非幂等</span>";
                        }}
                , {field: 'needfee', title: '是否收费', width: 100,templet:function(d){
                            return d.needfee==1?"<span class='layui-badge layui-bg-green'>收费</span>":"<span class='layui-badge layui-bg-red'>无效</span>";
                        }}
                , {title: '操作', width: 135, fixed:"right",toolbar:"#gateway-bar"}
            ]]
        });

        //给头部工具栏添加事件
        table.on("toolbar(gateway-table)", function(obj){
            switch (obj.event) {
                case "add":
                    openAddAndEditWindow(null);
                    break;
                case "delete":
                    //获取复选框选中行对应的id
                    var checkStatus =  table.checkStatus("gateway-table");
                    if(checkStatus.data.length>0){
                        var data = checkStatus.data;
                        var arr= [];
                        console.log(data)
                        for(var d in data){
                           arr.push(data[d].id)
                        }
                        //ids=1&ids=2&ids=3&ids=4  => int[] ids;
                        //
                        $.ajax({
                            url: "/apimapping/delete",
                            type: "delete",
                            data: JSON.stringify(arr),
                            contentType:"application/json",
                            success: function(ret){
                                if(ret.code==0){
                                    table.reload("gateway-table",{});
                                }else{
                                    layer.alert(ret.message);
                                }
                            }
                        })
                    }
                    else{
                        layer.alert("请至少选中一行!");
                    }
                    break;
            }
        })

        //给工具条添加事件
        table.on("tool(gateway-table)",function(obj){
            var data =  obj.data;
            switch (obj.event) {
                case "edit-gateway":
                    openAddAndEditWindow(data);
                    break;
            };

        });

        //添加表单提交事件
        form.on("submit(gateway-table)", function(data){
            console.log(data.field);//获取表单的数据
            table.reload("gateway-table",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where:data.field //添加的查询数据
            })
            return false;
        })

        function openAddAndEditWindow(data){
            //显示表单
            layer.open({
                type:1,
                title:data==null?"添加路由":"编辑路由",
                content: $("#editGateway").html(),
                area: ["500px", "400px"],
                btn: ["提交", "取消"],
                yes:function(index, layero){
                    //获取表单数据，
                    var formData = form.val("gateway-edit-form");

                    // 通过ajax提交到服务器
                    $.ajax({
                        url: data==null?"/apimapping/add":"/apimapping/edit",
                        type:data==null?"post":"put",
                        data: JSON.stringify(formData),
                        contentType: "application/json",
                        dataType:"json",
                        success: function(ret){
                            if(ret.code==0){
                                table.reload("gateway-table",{});
                                layer.close(index);
                            }else{
                                layer.alert(ret.message);
                            }

                            layer.closeAll('loading')
                        }
                    })

                },
                success:function(layero, index){
                    form.render();

                    if(data!=null){
                        form.val("gateway-edit-form", data);
                    }
                }
            })
        }

    });
</script>

<script type="text/html" id="gateway-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit-gateway">编辑</a>
</script>
<script type="text/html" id="editGateway">
    <form id="gateway-edit-form" style="width: 80%" class="layui-form" lay-filter="gateway-edit-form">
        <input type="hidden" name="id">
        <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
        <input type="text" name="gatewayapiname" required lay-verify="required" placeholder="请输入角色名"
    autocomplete="off"
    class="layui-input">
        </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">URL</label>
        <div class="layui-input-block">
        <input type="text" name="insideapiurl" required lay-verify="required" placeholder="请输入备注" autocomplete="off" class="layui-input">
        </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">serviceId</label>
        <div class="layui-input-block">
        <input type="text" name="serviceid" required lay-verify="required" placeholder="请输入备注" autocomplete="off" class="layui-input">
        </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
        <input type="text" name="description" required lay-verify="required" placeholder="请输入备注" autocomplete="off" class="layui-input">
        </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="state" title="有效" value="1" checked/>
                <input type="radio" name="state" title="无效" value="0"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否幂等</label>
            <div class="layui-input-block">
                <input type="radio" name="idempotents" title="幂等" value="1" checked/>
                <input type="radio" name="idempotents" title="非幂等" value="0"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否收费</label>
            <div class="layui-input-block">
                <input type="radio" name="needfee" title="收费" value="1" checked/>
                <input type="radio" name="needfee" title="免费" value="0"/>
            </div>
        </div>
        </form>
</script>
</body>
</html>